<template>
  <page-header-wrapper>
    <global-search :list="searchList" @search="handleSearch" @reset="handleReset"> </global-search>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onTableSelectChange }"
      >
        <template #tabAction>
          <y-button type="primary" icon="plus" @click="handleAdd">新建计划</y-button>
          <a-popconfirm placement="bottom" title="确定要忽略所选计划吗？" @confirm="handleIgnoreAll">
            <y-button type="warning">忽略所选</y-button>
          </a-popconfirm>
        </template>
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
        <ellipsis :length="20" tooltip slot="Name" slot-scope="text">{{ text || '/' }}</ellipsis>
        <span slot="time" slot-scope="text">{{ text | moment('YYYY-MM-DD') }} </span>
        <span slot="numberFormat" slot-scope="text">￥{{ text | NumberFormat }} </span>
        <div slot="action" slot-scope="text, record, index">
          <y-button type="link" @click="handleDetail(record)">查看</y-button>
          <y-button type="link" @click="handleEdit(record)">修改</y-button>
          <a-popconfirm placement="left" title="确定要忽略此计划吗？" @confirm="handleIgnore(index)">
            <y-button class="text-warning" type="link">忽略</y-button>
          </a-popconfirm>
          <a-popconfirm placement="left" title="确定要删除此计划吗？" @confirm="handleDel(record)">
            <y-button class="text-error" type="link">删除</y-button>
          </a-popconfirm>
        </div>
      </s-table>
    </a-card>
    <detail :visible.sync="visibleDetail" :data.sync="editData" />
    <planForm :visible.sync="visibleForm" :data.sync="editData" @success="tableRefresh"/>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import detail from './components/detail.vue'
import planForm from './components/form.vue'
import { getProjectPlanList, postProjectPlan } from '@/api/followUpManagement'
export default {
  components: {
    STable,
    Ellipsis,
    detail,
    planForm
  },
  data () {
    return {
      visibleDetail: false,
      visibleForm: false,
      editData: {},
      searchList: [
        {
          label: '年度查询',
          fields: 'Years',
          type: 'year',
          props: {
            placeholder: '请选择年份'
          }
        },
        {
          label: '计划名称',
          fields: 'Name',
          type: 'input',
          props: {
            placeholder: '请输入计划名称'
          }
        }
      ],
      queryParam: {
        strContent: {}
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' }
        },
        {
          title: '计划名称',
          align: 'center',
          width: '180px',
          dataIndex: 'Name',
          scopedSlots: { customRender: 'Name' }
        },
        {
          title: '开始时间',
          align: 'center',
          dataIndex: 'CreateDate',
          scopedSlots: { customRender: 'time' }
        },
        {
          title: '计划总套数',
          align: 'center',
          dataIndex: 'GoalPiece'
        },
        {
          title: '计划总面积',
          align: 'center',
          dataIndex: 'GoalArea'
        },
        {
          title: '预计使用资金',
          align: 'center',
          dataIndex: 'PredictFund',
          scopedSlots: { customRender: 'numberFormat' }
        },
        {
          title: '预计完成时间',
          align: 'center',
          dataIndex: 'PredictDate',
          scopedSlots: { customRender: 'time' }
        },
        {
          title: '操作',
          align: 'center',
          width: '210px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        this.selectedRowKeys = []
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getProjectPlanList(requestParameters).then((res) => res)
      },
      selectedRowKeys: []
    }
  },
  methods: {
    onTableSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleFilterAll () {},
    handleSearch (values) {
      this.queryParam.strContent = values
      this.tableRefresh(true)
    },
    handleReset () {
      this.queryParam.strContent = {}
      this.tableRefresh(true)
    },
    tableRefresh (refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    handleDetail (record) {
      this.editData = record
      this.visibleDetail = true
    },
    handleEdit (record) {
      this.editData = record
      this.visibleForm = true
    },
    handleAdd () {
      this.editData = {}
      this.visibleForm = true
    },
    handleIgnore (index) {
      this.$refs.table.localDataSource.splice(index, 1)
    },
    handleDel (record) {
      this.$loading.show({
        tip: '删除中，请稍后...'
      })
      postProjectPlan({
        State: 'delete',
        palns: {
          ID: record.ID
        }
      }).then(res => {
        this.$message.success('删除成功！')
        this.tableRefresh()
      }).finally(() => {
        this.$loading.hide()
      })
    },
    handleIgnoreAll () {
      if (this.$isEmpty(this.selectedRowKeys)) {
        this.$message.warning('至少选择一个计划！')
      } else {
        this.$refs.table.localDataSource = this.$refs.table.localDataSource.filter(e => !this.selectedRowKeys.includes(e.ID))
        this.selectedRowKeys = []
      }
    }
  }
}
</script>
<style scoped>
</style>
